<template>
  <el-dialog
    :title="`${isCreate ? '添加' : '编辑'}辅材`"
    :visible.sync="dialogVisible"
    class="dialog"
    width="400px">
    <el-form
      :model="formData"
      label-width="120px"
      ref="form"
      size="small">
      <el-form-item label="辅材名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="主图" prop="coverUrl">
        <uploader :src.sync="formData.coverUrl"></uploader>
      </el-form-item>
      <el-form-item label="售价（元）" prop="salePrice">
        <el-input-number v-model="formData.salePrice" :controls="false" :precision="2"></el-input-number>
      </el-form-item>
      <el-form-item label="原价（元）" prop="price">
        <el-input-number v-model="formData.price" :controls="false" :precision="2"></el-input-number>
      </el-form-item>
      <el-form-item label="运费（元）" prop="freightPrice">
        <el-input-number v-model="formData.freightPrice" :controls="false" :precision="2"></el-input-number>
      </el-form-item>
      <el-form-item label="关联课程" prop="courseId">
        <el-select
          filterable
          remote
          :remote-method="getCourseList"
          v-model="formData.courseId"
          placeholder="搜索课程">
          <el-option
            v-for="item in courseList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <dialog-footer
      slot="footer"
      @cancel="dialogVisible = false"
      @confirm="confirm"
    />
  </el-dialog>
</template>
<script>
import { saveSupplement, updateSupplement, getCourseList } from '@/api/course';

export default {
  props: {
    initFormData: Object,
    isCreate: Boolean,
  },
  data() {
    return {
      formData: {
        ...this.initFormData,
      },
      rules: {
        name: [{ required: true, message: '辅材名称不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '售价不能为空', trigger: 'blur' }],
        // salePrice: [{ required: true, message: '辅材名称不能为空', trigger: 'blur' }],
        freightPrice: [{ required: true, message: '运费不能为空', trigger: 'blur' }],
      },
      dialogVisible: false,
      courseList: [],
    };
  },
  watch: {
    initFormData(val) {
      this.formData = { ...val };
    },
    dialogVisible(val) {
      if (!this.isCreate && val) {
        console.log(this.initFormData);
        this.courseList = [
          {
            label: this.initFormData.courseName,
            value: this.initFormData.courseId,
          },
        ];
      } else {
        this.courseList = [];
      }
    },
  },
  methods: {
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const {
            coverUrl, price, salePrice, freightPrice, name, courseId,
          } = this.formData;
          if (coverUrl) {
            if (this.isCreate) {
              await saveSupplement({
                name,
                coverUrl,
                courseId,
                price: price * 100,
                salePrice: salePrice * 100,
                freightPrice: freightPrice * 100,
                valid: 1,
              });
            } else {
              await updateSupplement({
                ...this.formData,
                price: price * 100,
                salePrice: salePrice * 100,
                freightPrice: freightPrice * 100,
              });
            }
            this.dialogVisible = false;
            this.$emit('confirm', this.formData);
          } else {
            this.$message.error('主图不能为空');
          }
        }
      });
    },
    async getCourseList(query) {
      if (query) {
        const { list } = await getCourseList({
          name: query,
          pageSize: 50,
          pageNum: 0,
          valid: 1,
        });
        this.courseList = list.map((item) => ({
          label: item.name,
          value: item.id,
        }));
      } else {
        this.courseList = [];
      }
    },
  },
};
</script>
<style lang="scss" scoped>
  .dialog {
    ::v-deep .el-input__inner {
      width: 200px;
    }
  }
</style>
